import * as CONSTANT from './constant';
import fetch from '@xforceplus/httprequest';
import { message } from 'antd';

const loading = (boolean) => {
  return (dispatch, getState) => {
    dispatch({
      type: CONSTANT.LOADING,
      loading: boolean,
    });
  };
};
// searchFilter 筛选器开关
export const searchFilter = (boolean) => {
  return (dispatch, getState) => {
    dispatch({
      type: CONSTANT.SEARCHFILTER,
      boolean: boolean,
    });
  };
}

// getLogisticsRequestParam 查询条件
export const getLogisticsRequestParam = (param) => {//查询条件
  return (dispatch, getState) => {
    dispatch({
      type: CONSTANT.LOGISTICSREQUESTPARAM,
      logisticsRequestParam: param || {
        "batchNo": "",
        "bussinessNo": "",
        "bussinessType": "",
        "invoiceCode": "",
        "invoiceNo": "",
        "invoiceType": "",
        "receiverCompanyName": "",
        "receiverName": "",
        "receiverTel": "",
        "senderCompanyName": "",
        "senderName": "",
        "senderTel": "",
        "waybillNo": "",
        "createTimeScope": {
          "first": "",
          "second": ""
        },
        "amountWithTaxScope": {
          "first": "",
          "second": ""
        },
        "paperDrewDateScope": {
          "first": "",
          "second": ""
        },
        "senderTimeScope": {
          "first": "",
          "second": ""
        },
      },
    });
  };
}

// deleteParcelId
export const getDeleteParcelId = (param) => {
  return (dispatch, getState) => {
    dispatch({
      type: CONSTANT.DELETEPARCELID,
      newSendTypeList: param,
    });
  };
}

export const resetState = () => {
  return (dispatch, getState) => {
    dispatch({
      type: CONSTANT.RESETSTATE,
      cancelSendGoods: { status: false, list: "" }
    });
  };
}

// getOrderParam
export const getOrderParam = (param) => {// 排序方式
  return (dispatch, getState) => {
    dispatch({
      type: CONSTANT.ORDERPARAM,
      orderParam: param,
    });
  };
}

// getSendInLine待寄送 总数
export const getCountWait = (body) => {//待寄送 总数
  // console.log(`getCountWait========>`, body)
  return (dispatch, getState) => {
    dispatch(loading(true));
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logistics/countWaitSendGoods',
        body: body || {
          batchNo: '',//批次封面号
          bussinessNo: '',//业务单号
          invoiceNo: '',//发票号码
          paperDrewDate: [],//开票日期
          amountWithTax: [],//价税合计
        }
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.status === 500) {
          message.error("网络错误，请重试")
        }
        if (data && data.code === 0) {
          message.error(data.message)
        } else {
          dispatch({
            type: CONSTANT.COUNTWAIT,
            countWait: data.result
          })
        }
      });
    });
  };
};
// getCountParcel寄送中，异常，已签收 总数
export const getCountParcel = (body) => {//寄送中，异常，已签收
  // console.log('getCountParcel=====>', body)
  return (dispatch, getState) => {
    dispatch(loading(true));
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logisticsParcel/countParcel',
        body: body || {
          businessLgtStatus: 0,//包裹总数状态(3-寄送中，4-异常，5-已签收)
          parcelFlag: 0,
          waybillNo: '',//快递单号
          senderName: '',//寄件方
          senderTel: '',//寄件方电话
          receiverName: '',//收件方
          receiverTel: '',//收件方联系电话 
          senderTimeScope: { first: '', second: '' },//创建时间区间
        }
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.code === 0) {
          message.error(data.message)
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          dispatch({
            type: CONSTANT.CANCENEXPRESSSUCCESS,
            cancelExpressSuccess: false,
            chgwaybillNoBoolean: false
          })
          dispatch({
            type: CONSTANT.COUNTPARCEL,
            countParcel: data.result
          })
        }

      });
    });
  };
}

// getToSendList
export const getToSendList = (body) => {// 待寄件 列表
  // console.log(`getToSendList===>`, body)
  return (dispatch, getState) => {
    dispatch(loading(true));
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logistics/getWaitSendGoods',
        body: body || {
          logisticsGoodsRequestParam: {
            amountWithTaxScope: {
              first: "",
              second: ""
            },
            batchNo: "",
            bussinessNo: "",
            bussinessType: "",
            createTimeScope: {
              first: "",
              second: ""
            },
            invoiceCode: "",
            invoiceNo: "",
            invoiceType: "",
            paperDrewDateScope: {
              first: "",
              second: ""
            },
            receiverCompanyName: "",
            receiverName: "",
            receiverTel: "",
            senderCompanyName: ""
          },
          orderParam: {
            orderNum: 2
          },
          pageParam: {
            pageIndex: 1,
            pageRowCount: 10
          }
        }
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.code === 0) {
          message.error(data.message)
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          dispatch({
            type: CONSTANT.TOSENDLIST,
            toSendList: data.result
          })
        }
      });
    });
  };
};

// getParcel
export const getParcel = (body) => {//获取包裹
  // console.log(`getParcel====>`, body)
  return (dispatch, getState) => {
    dispatch(loading(true));
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logisticsParcel/getParcels',
        body: body || {
          logisticsParcelRequestParam: {
            businessLgtStatus: 0,
            parcelFlag: 0,
            receiverName: "",
            receiverTel: "",
            senderName: "",
            senderTel: "",
            senderTimeScope: {
              first: "",
              second: ""
            },
            waybillNo: ""
          },
          orderParam: {
            orderNum: 2
          },
          pageParam: {
            pageIndex: 1,
            pageRowCount: 10
          }
        }
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.code === 0) {
          message.error(data.message)
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          dispatch({
            type: CONSTANT.PARCEL,
            parcel: data.result
          })
        }
      });
    });
  };
}

// getParcelDetail
export const getParcelDetailById = (body) => {//包裹详情接口
  // console.log(`getParcelDetailById====>`, body)
  return (dispatch, getState) => {
    dispatch(loading(true));
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logisticsParcel/getParcelDetailById',
        body: body || 'id'
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.code === 0) {
          message.error(data.message)
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          dispatch({
            type: CONSTANT.PARCELDETAIL,
            parcelDetail: data.result
          })
          dispatch({
            type: CONSTANT.CANCENEXPRESSSUCCESS,
            cancelExpressSuccess: false,
            chgwaybillNoBoolean: false
          })
        }
      });
    });
  };
}

//cancelSendGoods
export const getCancelSendGoods = (body, boolean) => {//无需寄送接口-会调用两次
  // console.log(`getCancelSendGoods=>`, body)
  return (dispatch, getState) => {
    dispatch(loading(true));
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logistics/cancelSendGoods',
        body: body || {
          isAllSelect: '1',//1非全选， 2全选
          logisticsGoodsRequestParam: {
            batchNo: '',//批次封面号
            bussinessNo: '',//业务单号
            invoiceNo: '',//发票号码
            paperDrewDate: [],//开票日期
            amountWithTax: [],//价税合计    
          },
          id: [],
        }
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.code === 0) {
          message.error(data.message)
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          if (boolean) {
            dispatch({
              type: CONSTANT.CANCELSENDGOODS,
              cancelSendGoods: {
                status: false,
                list: data.result
              }
            })
            message.success(data.message)
          } else {
            dispatch({
              type: CONSTANT.CANCELSENDGOODS,
              cancelSendGoods: {
                status: true,
                list: data.result
              }
            })
          }
        }
      });
    });
  };
}

// getsended
export const getsended = (body) => {//寄送返回接口
  // console.log(`getsended=====>`, body)
  return (dispatch, getState) => {
    dispatch({ type: CONSTANT.SENDED, sended: [] })
    dispatch(loading(true));
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logistics/sendGoods',
        body: body || {
          isAllSelect: '1',//1非全选， 2全选
          logisticsGoodsRequestParam: {
            batchNo: '',//批次封面号
            bussinessNo: '',//业务单号
            invoiceNo: '',//发票号码
            paperDrewDate: [],//开票日期
            amountWithTax: [],//价税合计    
          },
          id: [],
        }
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.code === 0) {
          message.error(data.message)
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          data.result.map((item, index) => { item.id = index })
          dispatch({ type: CONSTANT.SENDED, sended: data.result })
          dispatch({ type: CONSTANT.CANCELSENDGOODS, cancelSendGoods: { status: false, list: "" } })
        }
      });
    });
  };
}

//getPartSendParcel
export const getPartSendParcel = (body) => {//分开寄送返回接口
  // console.log(`getPartSendParcel====>`, body)
  return (dispatch, getState) => {
    dispatch(loading(true));
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logistics/divideSendGoods',
        body: body || {
          isAllSelect: '',//1, 2(全部全选)
          goodsIdList: ['1', '2', '3'],
          logisticsGoodsRequestParam: {
            batchNo: '',//批次封面号
            bussinessNo: '',//业务单号
            invoiceNo: '',//发票号码
            paperDrewDate: [],//开票日期
            amountWithTax: [],//价税合计
          }
        }
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.code === 0) {
          message.error(data.message)
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          dispatch({
            type: CONSTANT.PARTSENDPARCEL,
            sendtype: data.result
          })
        }
      });
    });
  };
}

//getMergeSendParcel
export const getMergeSendParcel = (body) => {//合并寄送返回接口
  // console.log(`getMergeSendParcel===>`, body)
  return (dispatch, getState) => {
    dispatch(loading(true));
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logistics/mergeSendGoods',
        body: body || {
          isAllSelect: '',//1, 2(全部全选)
          goodsIdList: ['1', '2', '3'],
          logisticsGoodsRequestParam: {
            batchNo: '',//批次封面号
            bussinessNo: '',//业务单号
            invoiceNo: '',//发票号码
            paperDrewDate: [],//开票日期
            amountWithTax: [],//价税合计
          }
        }
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.code === 0) {
          message.error(data.message)
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          dispatch({
            type: CONSTANT.MERGESENDPARCEL,
            sendtype: data.result
          })
        }
      });
    });
  };
}

// getSubmitParcels
export const getSubmitParcels = (body) => {//提交包裹
  // console.log(`getSubmitParcels====>`, body)
  return (dispatch, getState) => {
    dispatch(loading(true));
    dispatch({
      type: CONSTANT.SUBMITPARCELS,
      submitParcels: []
    })
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logisticsParcel/submitParcels',
        body: body || []
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.code === 0) {
          message.error(data.message)
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          message.success(data.message)
          dispatch({
            type: CONSTANT.SUBMITPARCELS,
            submitParcels: data.result
          })
        }
      });
    });
  };
}

// getAlertWaybillNo
export const getAlertWaybillNo = (body) => {//修改运单号
  // console.log(`getAlertWaybillNo====>`, body)
  return (dispatch, getState) => {
    dispatch(loading(true));
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logisticsParcel/alterWaybillNo',
        body: body || {
          expressName: '',
          expressCode: '',
          expressLogo: '',
          waybillNo: '',
          goodsIds: [
            0
          ],
          isAllSelect: "",
          parcelId: 0,
        }
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.code === 0) {
          message.error(data.message)
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          dispatch({
            type: CONSTANT.CANCENEXPRESSSUCCESS,
            cancelExpressSuccess: true,
            chgwaybillNoBoolean: true
          })
          message.success(data.message)
        }
      });
    });
  };
}

// getCancelExpress
export const getCancelExpress = (body) => {//取消快递
  // console.log(`getCancelExpress====>`, body)
  return (dispatch, getState) => {
    dispatch(loading(true));
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logisticsParcel/cancelExpress',
        body: body || {
          isAllSignFlag: '1',//1部分 2全部
          parcelId: 0,
          notSignGoodsIds: [],
          signedGoodsIds: []
        }
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.code === 0) {
          message.error(data.message)
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          dispatch({
            type: CONSTANT.CANCENEXPRESSSUCCESS,
            cancelExpressSuccess: true,
            chgwaybillNoBoolean: false
          })
          message.success(data.message)
        }
      });
    });
  };
}

// getSignParcels
export const getSignParcels = (body) => {//签收
  // console.log(`getSignParcels====>`, body)
  return (dispatch, getState) => {
    dispatch(loading(true));
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logisticsParcel/signParcels',
        body: body || {
          isAllSignFlag: '1',//1部分 2全部
          parcelId: 0,
          notSignGoodsIds: [],
          signedGoodsIds: []
        }
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.code === 0) {
          message.error(data.message)
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          dispatch({
            type: CONSTANT.CANCENEXPRESSSUCCESS,
            cancelExpressSuccess: true,
            chgwaybillNoBoolean: false
          })
          message.success(data.message)
        }
      });
    });
  };
}

// getRetreatExpressByReceiver
export const getRetreatExpressByReceiver = (body) => {//退件
  // console.log(`getRetreatExpressByReceiver====>`, body)
  return (dispatch, getState) => {
    dispatch(loading(true));
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logisticsParcel/retreatExpressByReceiver',
        body: body || {
          isAllSignFlag: '1',//1部分 2全部
          parcelId: 0,
          notSignGoodsIds: [],
          signedGoodsIds: []
        }
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.code === 0) {
          message.error(data.message)
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          dispatch({
            type: CONSTANT.CANCENEXPRESSSUCCESS,
            cancelExpressSuccess: true,
            chgwaybillNoBoolean: false
          })
          message.success(data.message)
        }
      });
    });
  };
}

// getExpress
export const getExpress = () => {//获取快递公司信息
  return (dispatch, getState) => {
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logistics/getExpress',
        body: ''
      }).then((data) => {
        if (data && data.code === 0) {
          message.error(data.message)
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          dispatch({
            type: CONSTANT.EXPRESS,
            express: data.result
          })
        }
      });
    });
  };
}

// getParcelLgtResult
export const getParcelLgtResult = (body) => {//获取快递信息
  // console.log(`getParcelLgtResult====>`, body)
  return (dispatch, getState) => {
    dispatch(loading(true));
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/logistics/logisticsResult/getParcelLgtResult',
        body: body || {
          waybillNo: "",
          expressCode: ""
        }
      }).then((data) => {
        dispatch(loading(false));
        if (data && data.code === 0) {
          dispatch({
            type: CONSTANT.PARCELLGTRESULT,
            parcelLgtResult: {
              noSupport: data.message
            }
          })
        } else if (data && data.status === 500) {
          message.error("网络错误，请重试")
        } else {
          dispatch({
            type: CONSTANT.PARCELLGTRESULT,
            parcelLgtResult: data.result
          })
        }

      });
    });
  };
}

// getUsersourceapiUrls
export const getUsersourceapiUrls = (body) => {
  return (dispatch, getState) => {
    return new Promise((resolve, reject) => {
      return fetch({
        url: '/api/v1/ucenter/security/getUserResourceApiUrls',
        body: body || {
          "resourcePlatForm": "1",
          "rid": "1125432512"
        }
      }).then((data) => {
        if(data && data.code === 1){
          console.log(`资源码--->`, data.result)
          dispatch({
            type: CONSTANT.USERRESOURCE,
            userReSource: data.result
          })
        }else {
          message.error(data.message)
        }
      });
    });
  };
}

